﻿Imports BUS
Imports System.Data.SqlClient

Public Class frmpCongNhanVien

    Private oldNode As New TreeNode
    Private frmcho As DevExpress.Utils.WaitDialogForm

    Private Sub CreateTree()

        trvnhanvien.Nodes.Clear()

        Dim title = "Company"
        Dim tn As New TreeNode(title)
        tn.Tag = Nothing
        trvnhanvien.Nodes.Add(tn)

        Dim nodeAll As New TreeNode("Tất cả", 0, 0)
        nodeAll.Name = "0"
        nodeAll.Tag = Nothing
        tn.Nodes.Add(nodeAll)

        Dim dtBP As New DataTable
        dtBP = BoPhanBus.LayDanhSach()

        For Each rowbp As DataRow In dtBP.Rows
            Dim nodeBp As New TreeNode(rowbp("tenbp").ToString, 0, 0)
            nodeBp.Name = rowbp("mabp").ToString
            nodeBp.Tag = Nothing
            tn.Nodes.Add(nodeBp)
        Next

        'trvnhanvien.Sort()
        trvnhanvien.ExpandAll()

    End Sub

    Private Sub LayCongNV()
        Try
            Dim selNode As New TreeNode
            selNode = trvnhanvien.SelectedNode
            selNode.BackColor = Color.LightSkyBlue

            oldNode = selNode

            Dim sql As String = "exec sp_chamcongthangnhanvien " & nudthang.Value & ", " & nudnam.Value & ", N'" & cboloaicong.Text & "', '" & selNode.Name & "'"

            dgvcongnhanvien.DataSource = ProcFuncBus.ExecuteFuncSQL(sql)
        Catch ex As Exception
            Return
        End Try

    End Sub

    Private Sub frmCongNhanVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CreateTree()
        nudthang.Value = Date.Now.Month
        nudnam.Value = Date.Now.Year
        cboloaicong.SelectedIndex = 0
    End Sub

    Private Sub trvnhanvien_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvnhanvien.AfterSelect
        oldNode.BackColor = Color.Transparent
        LayCongNV()
    End Sub

    Private Sub nudthang_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudthang.ValueChanged
        LayCongNV()
    End Sub

    Private Sub nudnam_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudnam.ValueChanged
        LayCongNV()
    End Sub

    Private Sub cboloaicong_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboloaicong.SelectedIndexChanged
        LayCongNV()
    End Sub

    'Private Sub btnexcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    frmcho = New DevExpress.Utils.WaitDialogForm("Vui lòng chờ", "Đang xử lý")
    '    Dim rpt As New rptChamCongThang
    '    rpt.xrlTieuDe.Text = "BẢNG CHẤM CÔNG T" & nudthang.Value & " - " & nudnam.Value
    '    rpt.DataSource = dgvcongnhanvien.DataSource
    '    rpt.CreateDocument()
    '    rpt.ShowPreview()
    '    frmcho.Close()
    'End Sub
End Class